package com.example.exception;

import com.google.common.collect.Sets;

import java.util.Iterator;
import java.util.Set;

/**
 * ClassName: BusinessException
 * description: TODO
 *
 * @author wsy
 * @Date: 2022/11/20 12:11
 * @since JDK 1.8
 */
public class BusinessException extends RuntimeException {
    private String code;
    private Set<String> errors;

    public BusinessException() {
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
    }

    public BusinessException(String message) {
        super(message);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.errors.add(message);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.errors.add(message);
    }

    public BusinessException(Throwable cause) {
        super(cause);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.errors.add(cause.getMessage());
    }

    public BusinessException(String message, String code) {
        super(message);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.code = code;
        this.errors.add(message);
    }

    public BusinessException(String message, Throwable cause, String code) {
        super(message, cause);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.code = code;
        this.errors.add(message);
    }

    public BusinessException(Throwable cause, String code) {
        super(cause);
        this.code = CommonErrorCode.BUSINESS_ERROR.code();
        this.errors = Sets.newHashSet();
        this.code = code;
        this.errors.add(cause.getMessage());
    }


    public BusinessException addError(String msg) {
        this.errors.add(msg);
        return this;
    }

    public String getMessage() {
        String msg;
        if (this.errors.isEmpty()) {
            msg = super.getMessage();
        } else {
            StringBuilder sb = new StringBuilder(this.errors.size() * 15);
            Iterator var3 = this.errors.iterator();

            while (var3.hasNext()) {
                String message = (String) var3.next();
                sb.append(message).append(",");
            }

            sb.deleteCharAt(sb.length() - 1);
            msg = sb.toString();
        }

        return msg;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String code() {
        return this.code;
    }

    public String message() {
        return this.getMessage();
    }

    public static void throwIt(String message) {
        throw new BusinessException(message);
    }

    public static void throwIt(String message, String code) {
        throw new BusinessException(message, code);
    }

}
